//
//  DSEProbabilityHandle.m
//  qqqqqq
//
//  Created by iOSer on 2017/3/17.
//  Copyright © 2017年 iOSer. All rights reserved.
//
static int mulriple = 1000000;
#import "LEProbabilityHandle.h"
#import "NSArray+RandomSort.h"
@interface LEProbabilityHandle()
@property(nonatomic,assign) int mulriple;
@end
@implementation LEProbabilityHandle

+(NSString *)computeProbability:(NSArray *)array{
        int lastScope = 0;
        // 洗牌，打乱奖品次序
    
    
//        array = [array randomObjects];
//    NSMutableArray *shuffleArray = [[NSMutableArray alloc] initWithArray:array];
//    for (int i =0; i < 10000; i++) {
//        [shuffleArray addObject:[NSString stringWithFormat:@"%d", i]];
//    }
//    for (int i =0; i < shuffleArray.count; i++) {
//        int n = (arc4random() % (shuffleArray.count - i)) + i;
//        [shuffleArray exchangeObjectAtIndex:i withObjectAtIndex:n];
//    }
    
//    NSLog(@"%@, %p", array, array);
    
//    array = [array randomObjects];
    NSMutableArray *arrayM = [[NSMutableArray alloc] initWithArray:array];
//    for (int i = 0; i<arrayM.count; i++) {
//        
//        int j = (arc4random() % (arrayM.count-1));
//            NSDictionary * iDic = arrayM[i];
//            NSDictionary * jDic = arrayM[j];
//            [arrayM replaceObjectAtIndex:i withObject:jDic];
//            [arrayM replaceObjectAtIndex:j withObject:iDic];
//        
//        
//    }
    for (int i =0; i < arrayM.count; i++) {
        int n = (arc4random() % (arrayM.count - i)) + i;
        [arrayM exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
    NSMutableDictionary *commodityScopes = [[NSMutableDictionary alloc] initWithCapacity:42];
    
        for (NSDictionary *item in arrayM) {
            
            NSString *commodityId = item[@"id"];
            double commodityProbability = [item[@"Probability"] doubleValue];
            // 划分区间
            int currentScope = lastScope + (commodityProbability * mulriple);
//              @(lastScope+1),@(currentScope)
            [commodityScopes setValue:@{@"low":[NSNumber numberWithInt:lastScope+1],@"hig":[NSNumber numberWithInt:currentScope]} forKey:commodityId];
//            prizeScopes.put(prizeId, new int[] { lastScope + 1, currentScope });
            
            lastScope = currentScope;
        }
        
        // 获取1-1000000之间的一个随机数
//        srand( (unsigned int)time(0) );
//        int luckyNumber = (rand() % mulriple)+ mulriple;
        int luckyNumber =  (arc4random() % mulriple) + 1;
        __block NSString *luckyPrizeId = @"";
        // 查找随机数所在的区间
    
        if (commodityScopes) {
            
            for (NSDictionary *comDic in commodityScopes.allValues) {
                int arrOne = [comDic[@"low"] intValue];
                int arrTwo = [comDic[@"hig"] intValue];
                
                if ((luckyNumber >= arrOne) && (luckyNumber <= arrTwo)) {
                    
                    [commodityScopes enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                        
//                        NSLog(@"key = %@ and obj = %@", key, obj);
                        
                        if ([obj isEqualToDictionary:comDic]) {
                            
                             luckyPrizeId = key;
                            
                        }
                        
                    }];
                    
                    break;
                    
                    
                }
            }
//            for (int i = 0; i<commodityScopes.count; ++i) {
//                NSString *key = commodityScopes.allKeys[i];
//                NSArray *value = commodityScopes.allValues[i];
//                
//                if (luckyNumber >= (int)value[0] && luckyNumber <= (int)value[1]) {
//                    luckyPrizeId = key;
//                    break;
//                }
//            }
            
        }
        
    
        return luckyPrizeId;
}



@end
